package com.tianqu.tms.www.statistics.dao;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TmsTableNames;
import com.tianqu.tms.www.statistics.dto.ComboDto;
import com.tianqu.tms.www.statistics.entity.TmsMarketRegisterMonthCountDto;

public class TmsMarketRegisterMonthCountDao extends TmsDao{

	public TmsMarketRegisterMonthCountDao() throws TmsDaoException {
		super();
	}

	/**
	 * 最近0-9个月内新增入库/最近0-9个月内车位使用率
	 * @return
	 * @throws TmsDaoException
	 */
	public TmsMarketRegisterMonthCountDto get09MonthNewStorageAndParkingRate() throws TmsDaoException {

		PreparedStatement ps = null;
		ResultSet rs = null;
		
		TmsMarketRegisterMonthCountDto dto = new TmsMarketRegisterMonthCountDto();
		List<ComboDto> comboDtoList1 = new ArrayList<ComboDto>();
		List<ComboDto> comboDtoList2 = new ArrayList<ComboDto>();
		java.text.DecimalFormat myformat = new java.text.DecimalFormat("0.0");

		try {

			StringBuffer sql = new StringBuffer("SELECT");
			sql.append(" concat(tmrmc.countY, LPAD(tmrmc.countM, 2, 0)) as resDate,");
			sql.append(" tmrmc.regNum,");
			sql.append(" tmrmc.aveRatio");
			sql.append(" FROM ");
			sql.append(TmsTableNames.TMS_MARKET_REGISTER_MONTH_COUNT);
			sql.append(" tmrmc ");
			sql.append(" order by resDate desc");
			sql.append(" limit 0,9");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();

			while (rs.next()) {
				ComboDto dto1 = new ComboDto();
				dto1.setName(rs.getString("resDate"));
				dto1.setValue(rs.getString("regNum"));
				comboDtoList1.add(dto1);

				ComboDto dto2 = new ComboDto();
				dto2.setName(rs.getString("resDate"));
				dto2.setValue(String.valueOf(new BigDecimal(100).multiply(rs.getBigDecimal("aveRatio"))));
				comboDtoList2.add(dto2);
			}

			dto.setMonthNewStorageList(comboDtoList1);
			dto.setMonthParkingRateList(comboDtoList2);

			return dto;

		} catch (Exception e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}
}
